<?php
// +----------------------------------------------------------------------
// | 针对跑数据的处理
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2016-3-31 下午2:17:05
// +----------------------------------------------------------------------

class Rundata extends MY_Controller{
	
	//1940947
	public function changenDate(){
		exit('完成');
		$id = $this->uri->segment(3, 0);
		$befor_id = empty( $id ) ? 0 : $id;
		if ( $befor_id > '1522025' ){
			exit('完成');
		}
		$offset = 1000;
		$this->load->model('Tmsearchnew_model', 'tmsearch');
// 		$where = array('id >= '=> $befor_id);
		$where = array('id >= '=>$befor_id,'trends <>'=> '');
// 		$field = 'id,day,guojidate,firstdate,lastdate,shenqingdate,gonggaodate,zhucedate,chushendate,enddate,trends';
		$field = 'id,trends';
		$result = $this->tmsearch->getDataObj($where, $field, $offset, 0, 'id ASC');
		foreach ( $result as $key => $val ){
			echo '<br>'.$val['id'].'<br>';		
// 			if ( !is_null($val['day']) && $this->checkDateIsValid($val['day']) == false  ){
// 				$day = substr($val['day'], 0,-3);
// 				$day_date = date('Y-m-d', $day);
// 				$update['day'] = intval($day_date) == 1970 ? '' : $day_date;				
// 			}
// 			if ( !is_null($val['shenqingdate']) && $this->checkDateIsValid($val['shenqingdate']) == false ){
// 				$shenqing = substr($val['shenqingdate'], 0,-3);
// 				$shenqing_date = date('Y-m-d', $shenqing);
// 				$update['shenqingdate'] = intval($shenqing_date) == 1970 ? '' : $shenqing_date ;
// 			}
// 			if ( !is_null($val['guojidate']) && $this->checkDateIsValid($val['guojidate']) == false ){
// 				$guojidate = substr($val['guojidate'], 0,-3);
// 				$guojidate_date =  date('Y-m-d', $guojidate);
// 				$update['guojidate'] = intval( $guojidate_date )== 1970 ? '' : $guojidate_date;
// 			}
			
// 			if (!is_null($val['firstdate']) && $this->checkDateIsValid($val['firstdate']) == false ){
// 				$firstdate = substr($val['firstdate'], 0,-3);
// 				$firstdate_date = date('Y-m-d', $firstdate);				
// 				$update['firstdate'] = intval( $firstdate_date ) == 1970 ? '' : $firstdate_date;
// 			}
// 			if (!is_null($val['lastdate']) && $this->checkDateIsValid($val['lastdate']) == false ){
// 				$lastdate = substr($val['lastdate'], 0,-3);
// 				$lastdate_date = date('Y-m-d', $lastdate);
// 				$update['lastdate'] = intval( $lastdate_date ) == 1970 ? '' : $lastdate_date;
// 			}
// 			if (!is_null($val['gonggaodate']) && $this->checkDateIsValid($val['gonggaodate']) == false ){
// 				$gonggaodate = substr($val['gonggaodate'], 0,-3);
// 				$gonggaodate_date = date('Y-m-d', $gonggaodate);
// 				$update['gonggaodate'] = intval( $gonggaodate_date ) == 1970 ? '' : $gonggaodate_date ;
// 			}
// 			if (!is_null($val['zhucedate']) && $this->checkDateIsValid($val['zhucedate']) == false ){
// 				$zhucedate = substr($val['zhucedate'], 0,-3);
// 				$zhucedate_date = date('Y-m-d', $zhucedate);
// 				$update['zhucedate'] = intval($zhucedate_date) == 1970 ? '' : $zhucedate_date;
// 			}
// 			if (!is_null($val['chushendate']) && $this->checkDateIsValid($val['chushendate']) == false ){
// 				$chushendate = substr($val['chushendate'], 0,-3);
// 				$chushendate_date = date('Y-m-d', $chushendate);
// 				$update['chushendate'] = intval( $chushendate_date ) == 1970 ? '' : $chushendate_date;
// 			}
// 			if (!is_null($val['enddate']) && $this->checkDateIsValid($val['enddate']) == false ){
// 				$enddate = substr($val['enddate'], 0,-3);
// 				$enddate_date = date('Y-m-d', $enddate);
// 				$update['enddate'] = intval( $enddate_date )==1970 ? '' : $enddate_date ;
// 			}
// 			if ( !empty( $val['trends'] ) ){
// 				$array = explode('|', $val['trends']);
// 				$count = count( $array );
// 				for ( $i=0; $i<$count; $i++ ){
// 					$array_second = explode(":", $array[$i]);
// 					$date_new = date('Y-m-d', substr($array_second[1], 0, -3)) ;
// 					$date_second = intval($date_new) == 1970 ? $array_second[1] : $date_new;
// 					$new_second  = $array_second[0].':'.$date_second;
// 					$new_array[$i]   = $array_second[0].':'.$date_second;
// 				}
// 				$update['trends'] = implode("|", $new_array);
// 			}
			if ( !empty( $val['trends'] ) ){
				$array = explode(',', substr($val['trends'], 0, -1));
				$count = count( $array );
				for ( $i=0; $i<$count; $i++ ){
					if ( is_numeric($array[$i]) && strlen( $array[$i] )> 5 ){
						$date_new = date('Y-m-d', substr($array[$i], 0, -3));
						$new_array[$i] = $array[$i-1].":".$date_new;
					}
				}
				$update['trends'] = implode("|", $new_array);
			}
			print_r( $update );
			echo '<br>';
			if ( !empty( $update ) ){
				$state = $this->tmsearch->updateData( array('id'=>$val['id']), $update );
				if ( $state ){
					echo '更新成功'.$val['id'].'<br>';
				}else{
					echo '更新失败'.$val['id'].'<br>';
				}
			}
		}
		$this->load->vars('id', $offset+$id);
		$this->load->view('rundata/changedate');
		
	}
	
	
	public function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d")) {
	    $unixTime = strtotime($date);
	    if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
	        return false;
	    }
	    //校验日期的有效性，只要满足其中一个格式就OK
	    foreach ($formats as $format) {
	        if (date($format, $unixTime) == $date) {
	            return true;
	        }
	    }
    	return false;
	}
}